#coding=utf8
from PySide2.QtWidgets import QApplication, QMessageBox, QWidget, QPushButton, QVBoxLayout, QFileDialog, QApplication, QLineEdit, QTreeView, QHBoxLayout,QTableWidgetItem
from PySide2.QtUiTools import QUiLoader
from PySide2 import QtWidgets
from PySide2 import QtCore
from PySide2.QtCore import QTimer,QDateTime
from PySide2 import QtGui 
from PySide2.QtGui import *
import sys 
import os
import jieba
import openpyxl
import pandas as pd
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
import matplotlib.pyplot as plt
import numpy as np
from os_cipin import Getword
import string
# 导入文件
class IMpoetTXT(QtWidgets.QDialog):
    def __init__(self):
        super().__init__()
        #获取系统所有文件
        self.model01 = QtWidgets.QFileSystemModel()
        #进行筛选只显示文件夹，不显示文件和特色文件
        self.model01.setFilter(QtCore.QDir.Dirs | QtCore.QDir.NoDotAndDotDot)
        self.model01.setRootPath('')
        self.enter = 0

        #定义创建左边窗口
        self.treeView1 = QTreeView(self)
        self.treeView1.setModel(self.model01)
        for col in range(1, 4):
            self.treeView1.setColumnHidden(col, True)
        self.treeView1.doubleClicked.connect(self.initUI)
 
        #定义创建右边窗口
        self.model02 = QStandardItemModel()
        self.treeView2 = QTreeView(self)
        self.treeView2.setModel(self.model02)
        self.treeView2.doubleClicked.connect(self.initUI2)
        #将创建的窗口进行添加
        self.layout = QHBoxLayout()
        #self.resize(1024, 400)
        self.layout.addWidget(self.treeView1)
        self.layout.addWidget(self.treeView2)
        self.setLayout(self.layout)
        self.filePath = ''

    # 主要是对m2进行的操作
    def initUI(self, Qmodelidx):
        #每次点击清空右边窗口数据
        self.model02.clear()
        #定义一个数组存储路径下的所有文件
        PathData = []
        #获取双击后的指定路径
        self.filePath = self.model01.filePath(Qmodelidx)
        # List窗口文件赋值
        PathDataName = self.model02.invisibleRootItem()
        #拿到文件夹下的所有文件
        PathDataSet = os.listdir(self.filePath)
        #进行将拿到的数据进行排序
        PathDataSet.sort()
        #遍历判断拿到的文件是文件夹还是文件，Flase为文件，True为文件夹
        '''
        for Data in range(len(PathDataSet)):
            if os.path.isdir(filePath + '\\' + PathDataSet[Data]) == False:
                print('拿到的是文件')
                PathData.append(PathDataSet[Data])
            elif os.path.isdir(filePath + '\\' + PathDataSet[Data]) == True:
                print('拿到的是文件夹')
        '''
        for Data in range(len(PathDataSet)):
            file_post = str(PathDataSet[Data].split('.')[-1])
            if (file_post == 'txt')|(file_post =='docx')|(file_post =='doc')|(file_post == 'wps')|(file_post == 'wpt')|(file_post == 'dot')|(file_post == 'rtf')|(file_post == 'docx')|(file_post == 'docm')|(file_post == 'dotm')|(file_post == 'xml')|(file_post == 'mht')|(file_post == 'mhtml')|(file_post == 'html')|(file_post == 'htm')|(file_post == 'pdf'):
                PathData.append(PathDataSet[Data])
                #print(Data)
     
        #将拿到的所有文件放到数组中进行右边窗口赋值。
        for got in range(len(PathData)):
            gosData = QStandardItem(PathData[got])
            PathDataName.setChild(got, gosData)

    def initUI2(self, Qmodelidx):
    
        self.enter = 1
        self.ll = self.model02.itemFromIndex(Qmodelidx)#<PySide2.QtGui.QStandardItem object at 0x000002A85C0883C8>
        #print(self.ll.data(0)) #获得文件名字
        self.close()

    def getLocationFile(self):
        if(self.filePath == '') | (self.enter == 0):
            return ''
        return self.filePath + '/' + self.ll.data(0)
# 导入文件夹
class OutputTXTFolder(QtWidgets.QDialog):
    def __init__(self):
        super().__init__()
        #获取系统所有文件
        self.model01 = QtWidgets.QFileSystemModel()
        #进行筛选只显示文件夹，不显示文件和特色文件
        self.model01.setFilter(QtCore.QDir.Dirs | QtCore.QDir.NoDotAndDotDot)
        self.model01.setRootPath('')
 
        #定义创建左边窗口
        self.treeView1 = QTreeView(self)
        self.treeView1.setModel(self.model01)
        for col in range(1, 4):
            self.treeView1.setColumnHidden(col, True)
        
 
        self.button = QPushButton("确定",self)
        #将创建的窗口进行添加verticalLayout
        self.layout = QVBoxLayout()
        #self.resize(1024, 400)
        self.layout.addWidget(self.button)
        self.layout.addWidget(self.treeView1)

        self.setLayout(self.layout)

        self.treeView1.doubleClicked.connect(self.initUI)
        self.button.clicked.connect(self.pushCorrect)

    # 主要是对m2进行的操作
    def initUI(self, Qmodelidx):
        #获取双击后的指定路径
        self.filePath = self.model01.filePath(Qmodelidx)


    def pushCorrect(self):
        self.close()
    
    # 返回路径   
    def getstrAddr(self):
        PathData = []
        PathDataSet = os.listdir(self.filePath)
        #进行将拿到的数据进行排序
        PathDataSet.sort()
        #遍历判断拿到的文件是文件夹还是文件，Flase为文件，True为文件夹
        for Data in range(len(PathDataSet)):
            file_post = str(PathDataSet[Data].split('.')[-1])
            if (file_post == 'txt')|(file_post == 'doc')|(file_post == 'wps')|(file_post == 'wpt')|(file_post == 'dot')|(file_post == 'rtf')|(file_post == 'docx')|(file_post == 'dotx')|(file_post == 'docm')|(file_post == 'dotm')|(file_post == 'xml')|(file_post == 'mht')|(file_post == 'mhtml')|(file_post == 'html')|(file_post == 'htm')|(file_post == 'pdf'):
                PathData.append(self.filePath + '/' + PathDataSet[Data])
        return PathData
# 输出文件夹
class OutputTXT(QtWidgets.QDialog):
    def __init__(self):
        super().__init__()
        self.filename = ''
        self.filePath = ''
        #获取系统所有文件
        self.model01 = QtWidgets.QFileSystemModel()
        #进行筛选只显示文件夹，不显示文件和特色文件
        self.model01.setFilter(QtCore.QDir.Dirs | QtCore.QDir.NoDotAndDotDot)
        self.model01.setRootPath('')
 
        #定义创建左边窗口
        self.treeView1 = QTreeView(self)
        self.treeView1.setModel(self.model01)
        for col in range(1, 4):
            self.treeView1.setColumnHidden(col, True)
        
 
        self.line = QLineEdit(self)
        self.button = QPushButton("确定",self)
        #将创建的窗口进行添加verticalLayout
        self.layoutH = QHBoxLayout()
        self.layout = QVBoxLayout()
        #self.resize(1024, 400)
        self.layoutH.addWidget(self.line)
        self.layoutH.addWidget(self.button)

        self.layout.addWidget(self.treeView1)
        self.layout.addLayout(self.layoutH)
        self.setLayout(self.layout)

        self.treeView1.doubleClicked.connect(self.initUI)
        self.button.clicked.connect(self.pushCorrect)
        self.line.returnPressed.connect(self.pushCorrect)
    # 主要是对m2进行的操作
    def initUI(self, Qmodelidx):
        #获取双击后的指定路径
        self.filePath = self.model01.filePath(Qmodelidx)
        # print(self.filePath)

    def pushCorrect(self):
        self.filename = self.line.text()
        self.close()
    
    # 返回路径   
    def getstrAddr(self):
        if (self.filename == '') | (self.filePath == ''):
            return ''
        return self.filePath +'/' + self.filename

class Stats:

    def __init__(self):
        # 从文件中加载UI定义
        self.ui = QUiLoader().load('ui/show.ui')
        self.ui.mainWidget.setCurrentIndex(0)
        self.ui.radioButtonforL.setChecked(True)

        palette1 = QtGui.QPalette()
        palette1.setColor(palette1.Background,QtGui.QColor(50,50,50))
        self.ui.setPalette(palette1)

        self.ui.tablecipinWidget.horizontalHeader().setStyleSheet("QHeaderView::section{background-color:lightblue;color: black;padding-left: 4px;border: 1px solid #6c6c6c;}")
        self.ui.tablecipinWidget.verticalHeader().setStyleSheet("QHeaderView::section{background-color:lightblue;color: black;padding-left: 4px;border: 1px solid #6c6c6c;}")
        # 界面内容提示
        self.ui.readCtrlCEdit.setPlaceholderText('请在这里输入文本')
        self.ui.tablecipinWidget.horizontalHeader().setStretchLastSection(True)

        # 信号连接
        self.ui.statisticsButton.clicked.connect(self.handstatisticsButton)

        self.ui.importfileButton.clicked.connect(self.handimportfileButton)
        self.ui.importfilesButton.clicked.connect(self.handimportfilesButton)
        self.ui.listWidget.clicked.connect(self.handfilesstatic)
        
        self.ui.outPutButton.clicked.connect(self.handOutputButton)
        self.ui.showstaticsButton.clicked.connect(self.handShowstaticsButton)
        self.ui.analyseButton.clicked.connect(self.handAnalyseButton)
        self.ui.workButton.clicked.connect(self.handWorkButton)
        
        #第三页任务栏 
        self.ui.updataButton.clicked.connect(self.handUpdataButton)
        self.ui.lineEditminInput.returnPressed.connect(self.handLineEditminInput)
        self.ui.lineEditmaxInput.returnPressed.connect(self.handLineEditmaxInput)
        self.ui.radioButtonforL.toggled.connect(self.handRadioButtonforL)
        self.ui.serchButton.clicked.connect(self.handSerchButton)
        # 画图部分
        self.ui.graphPushButton.clicked.connect(self.handGraphPushButton)

        self.figure = plt.figure(facecolor='#FFD7C4') #可选参数,facecolor为背景颜色
        self.graph = FigureCanvas(self.figure)
        self.ui.verticalLayoutpic.addWidget(self.graph)
        # 分析参数设置
        self.min = 0
        self.max = 10000000
        self.ui.lineEditminInput.setText('0')
        self.ui.lineEditmaxInput.setText('10000000')
        self.sortmode = 1 # 默认为降序

        # 定时器更新进度
        self.Timer=QTimer()
        self.Timer.timeout.connect(self.timeUpData)
    # 读取文件列表
    def handimportfileButton(self):
        self.ui.textBrowser.append("导入文件")
        self.dlg = IMpoetTXT()        
        # 显示对话框，代码阻塞在这里，等待对话框关闭后，才能继续往后执行
        self.dlg.exec_()
        if(self.dlg.getLocationFile() != ''):
            self.ui.listWidget.addItem(self.dlg.getLocationFile())
            self.ui.textBrowser.append("导入文件完毕！")
        else:
            self.ui.textBrowser.append("导入文件失败！")

    # 处理导入文件夹
    def handimportfilesButton(self):
        self.ui.textBrowser.append("导入文件夹内所有txt\docx文件")
        self.folderadd = OutputTXTFolder()
        self.folderadd.exec_()
        #print(self.folderadd.getstrAddr())
        foderlist = self.folderadd.getstrAddr()
        if len(foderlist) == 0:
            self.ui.textBrowser.append("导入文件夹内没有任何txt\docx文件！")
        else:# 导入文件目录
            for i in range(len(foderlist)):
                self.ui.listWidget.addItem(foderlist[i])
            self.ui.textBrowser.append("导入文件夹内所有txt\docx文件完毕！")

    # 处理listWidget-文件夹选择状态
    def handfilesstatic(self):
        
        if(self.ui.listWidget.currentItem().backgroundColor() != QColor(63, 63, 63)):#当前为系统的就是选中
            self.ui.listWidget.currentItem().setBackground(QColor(63, 63, 63))#
            self.ui.textBrowser.append("已取消选中文件" + self.ui.listWidget.currentItem().data(0))
        else:#当前为应该选中的
            self.ui.listWidget.currentItem().setBackground(QColor(200, 200, 0))
            self.ui.textBrowser.append("已选中文件" + self.ui.listWidget.currentItem().data(0))
        

    #statisticsButton的回调函数
    def handstatisticsButton(self):
        
        self.ui.statisticsButton.setEnabled(False)
        # 当前软件是否有选中的文件
        listFileLocation = []
        for i in range(self.ui.listWidget.count()):
            if self.ui.listWidget.item(i).backgroundColor() != QColor(200, 200, 0):
                continue
            listFileLocation.append(self.ui.listWidget.item(i).data(0))
        if len(listFileLocation) == 0:
            self.ui.textBrowser.append("未选中任何文档！")
            if(self.ui.readCtrlCEdit.toPlainText() == ''):
                self.ui.statisticsButton.setEnabled(True)
                self.ui.textBrowser.append("输入框也无任何文本！")
                return
            self.ui.textBrowser.append("正在统计对话框的词频！")
            article = self.ui.readCtrlCEdit.toPlainText()
            

            if(self.ui.signalCheckBox.isChecked()):
                article = article.replace('-',' ')       #连字符—用空格代替
            words = article.split()               #字符串按空格分割--分词
            #迭代处理：将字典变列表，存入数据
            hist= {}
            staticmodul = 0
            if(self.ui.bigsmallCheckBox.isChecked()):
                staticmodul = 1
            for i in range(len(words)):
                words[i] = words[i].strip(string.punctuation)  #去掉标点符号，去掉首尾
                if(staticmodul & 1 == 0): #末尾为1 模式为区分大小写为0就是不区分
                    words[i] = words[i].lower()                    #统一大小写
                if words[i] in hist:                          #统计词频与单词
                    hist[words[i]] = hist[words[i]] + 1        #不是第一次
                else:
                    hist[words[i]] = 1                         #第一次
                self.articlelist = sorted(hist.items(),key = lambda x:x[1], reverse = True)
                self.ui.statisticsButton.setEnabled(True)
                self.ui.textBrowser.append("统计词频完毕！")
        else:    
            self.ui.textBrowser.append("正在统计文件的词频！")
            modulcut = 0
            if(self.ui.bigsmallCheckBox.isChecked()):
                modulcut = 1
            if(self.ui.signalCheckBox.isChecked()):
                modulcut += 10   
            self.GWord = Getword()
            self.GWord.openFile(listFileLocation, modulcut)
            self.Timer.start(50)

    def handPressedEnter(self):
        #print("enter")
        cc = 1
    
    # 输出回调函数
    def handOutputButton(self):
        # 获取路径
        self.seavefile = OutputTXT()
        self.seavefile.exec_()
        sevepath = self.seavefile.getstrAddr()
        if(sevepath == ''):
            return
        wb = openpyxl.Workbook()
        columnHeaders = []
        # create column header list
        for j in range(self.ui.tablecipinWidget.columnCount()):
            columnHeaders.append(self.ui.tablecipinWidget.horizontalHeaderItem(j).text())

        df = pd.DataFrame(columns=columnHeaders)
        # create dataframe object recordset
        for row in range(self.ui.tablecipinWidget.rowCount()):
            for col in range(self.ui.tablecipinWidget.columnCount()):
                item = self.ui.tablecipinWidget.item(row, col)
                df.at[row, columnHeaders[col]] = item.text()
        
        df.to_csv(sevepath + '.txt',sep=",",index=False,encoding='utf-8')
        df.to_excel(sevepath + '.xlsx', index=False)
        #os.system(sevepath + '.xlsx')

    # 切换展示show的按钮
    def handShowstaticsButton(self):    
        self.ui.mainWidget.setCurrentIndex(2)

    # 切换分析界面按钮
    def handAnalyseButton(self):
        self.ui.mainWidget.setCurrentIndex(1)
    # 切换工作按钮
    def handWorkButton(self):
        self.ui.mainWidget.setCurrentIndex(0)

    # 更新按钮回调函数
    def handUpdataButton(self):
        # 先显示一下
        self.ui.tablecipinWidget.clearContents()
        self.min = int(self.ui.lineEditminInput.text())
        self.max = int(self.ui.lineEditmaxInput.text())
        if(self.sortmode == 0): #设定为降序
            num = 0
            for i in range(len(self.articlelist)):
                row = len(self.articlelist) - 1 - i
                itemL = QTableWidgetItem()
                itemR = QTableWidgetItem()
                itemL.setText(self.articlelist[row][0])
                itemR.setText(str(self.articlelist[row][1]))
                if(self.articlelist[row][1] < self.min) | (self.articlelist[row][1] > self.max):
                    continue
                self.ui.tablecipinWidget.insertRow(num)
                self.ui.tablecipinWidget.setItem(num, 0, itemL)
                self.ui.tablecipinWidget.setItem(num, 1, itemR)
                num += 1
            self.ui.tablecipinWidget.setRowCount(num)
            return
        num = 0
        for i in range(len(self.articlelist)):
            itemL = QTableWidgetItem()
            itemR = QTableWidgetItem()
            
            itemL.setText(self.articlelist[i][0])
            itemR.setText(str(self.articlelist[i][1]))
            if(self.articlelist[i][1] < self.min) | (self.articlelist[i][1] > self.max):
                continue
            self.ui.tablecipinWidget.insertRow(num)
            self.ui.tablecipinWidget.setItem(num, 0, itemL)
            self.ui.tablecipinWidget.setItem(num, 1, itemR)
            num += 1
        self.ui.tablecipinWidget.setRowCount(num)

    def handLineEditminInput(self):
        self.min = int(self.ui.lineEditminInput.text())
        #print(min)
    def handLineEditmaxInput(self):
        self.max = int(self.ui.lineEditmaxInput.text())
        #print(max)
    def handRadioButtonforL(self):
        if(self.ui.radioButtonforL.isChecked() == True):
            self.sortmode = 1 #设定为升序
        else:
            self.sortmode = 0 #设定为降序

    def handGraphPushButton(self):
        plt.clf()
        rowCountT = self.ui.tablecipinWidget.rowCount()# 行
        colCountT = self.ui.tablecipinWidget.columnCount()# 列
        
        

        numList = []
        danciList = []
        for i in range(rowCountT):
            if(self.ui.tablecipinWidget.item(i,1).text() == ''):
                 break
            numList.append(int(self.ui.tablecipinWidget.item(i,1).text()))
            danciList.append(self.ui.tablecipinWidget.item(i,0).text())
            
        #将AgeList中的数据转化为int类型
        numList = list(map(int, numList))

        # 将x,y轴转化为矩阵式
        self.x = np.arange(len(danciList)) + 1
        self.y = np.array(numList)

        #tick_label后边跟x轴上的值，（可选选项：color后面跟柱型的颜色，width后边跟柱体的宽度）
        plt.bar(range(len(danciList)), numList, tick_label=danciList, color='green', width=0.5)
        plt.xticks(rotation = 60)
        # 在柱体上显示数据
        for a, b in zip(self.x, self.y):
            plt.text(a-1, b, '%d' % b, ha='center', va='bottom')

        #设置标题
        plt.title("Demo")
		
		#画图
        self.graph.draw()
        #plt.savefig('1.jpg')
        #print("绘画图形")
    def timeUpData(self):
        staticjudge,self.articlelist = self.GWord.getHistList()
        if(staticjudge == False):
            return
        self.articlelist = sorted(self.articlelist.items(),key = lambda x:x[1], reverse = True)
        #print(self.articlelist)
        self.ui.textBrowser.append("统计词频完毕！")
        self.ui.textBrowser.append("请点击显示进行查看")
        self.ui.statisticsButton.setEnabled(True)
        self.Timer.stop()

    def handSerchButton(self):
        self.ui.textBrowserfortongji.append("正在查找请稍后")
        text = self.ui.lineEditserchInput.text()
        if(text == ''):
            self.ui.textBrowserfortongji.append("未输入，请再次输入！")
            return
        for i in range(len(self.articlelist)):
            if(self.articlelist[i][0] == text):
                self.ui.textBrowserfortongji.append(text+":"+str(self.articlelist[i][1]))
                self.ui.textBrowserfortongji.append("查找成功！")
                return
        self.ui.textBrowserfortongji.append("输入不存在！")
        
